package com.example.lanou.mynews.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.lanou.mynews.R;
import com.example.lanou.mynews.adapter.MainFragAdapter;

/**
 * 是主要Activity，是最外层tablayout
 */

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private MainFragAdapter adapter;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = (Toolbar) findViewById(R.id.activity_main_toolbar);
        toolbar.setBackgroundColor(Color.RED);
        toolbar.setLogo(R.mipmap.logo);
        adapter = new MainFragAdapter(getSupportFragmentManager(),this);
        tabLayout= (TabLayout) findViewById(R.id.activity_main_tablayout);
        viewPager = (ViewPager) findViewById(R.id.activity_main_vp);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setSelectedTabIndicatorHeight(0);
        viewPager.addOnPageChangeListener(this);
        for(int i=0;i<tabLayout.getTabCount();i++){
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if(tab!=null){
                tab.setCustomView(adapter.getTabView(i));
            }
        }


    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        Log.d("MainActivity", "position:" + position);
        Log.d("MainActivity", "positionOffset:" + positionOffset);
        Log.d("MainActivity", "positionOffsetPixels:" + positionOffsetPixels);

    }

    @Override
    public void onPageSelected(int position) {
        if(position==0||position==3){
            toolbar.setVisibility(View.VISIBLE);
        }else {
            toolbar.setVisibility(View.GONE);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        Log.d("MainActivity", "state:" + state);

    }
}
